package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.Map;

public class InfoActivity extends BaseActivity {


    private Button nan_check;

    private Button nan_checked;

    private Button nv_check;

    private Button nv_checked;

    private Integer sex = 0;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info_activity);
        register();
        checkReturn();
        checkSex();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.info_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }
    private void checkSex() {
        nan_check = findViewById(R.id.nan_check);
        nan_checked = findViewById(R.id.nan_checked);
        nv_check = findViewById(R.id.nv_check);
        nv_checked = findViewById(R.id.nv_checked);
        nan_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nan_check.setVisibility(View.INVISIBLE);
                nv_checked.setVisibility(View.INVISIBLE);
                nv_check.setVisibility(View.VISIBLE);
                nan_checked.setVisibility(View.VISIBLE);
                sex = 1;

            }
        });

        nv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nan_check.setVisibility(View.VISIBLE);
                nv_checked.setVisibility(View.VISIBLE);
                nv_check.setVisibility(View.INVISIBLE);
                nan_checked.setVisibility(View.INVISIBLE);
                sex = 2;
            }
        });
    }

    /**
     * 判断是否为数字
     * @param str 字符串
     * @return 结果
     */
    public static boolean isNumeric(final String str) {
        // null or empty
        if (str == null || str.length() == 0) {
            return false;
        }
        return str.chars().allMatch(Character::isDigit);
    }

    /**
     * 注册
     */
    private void register() {
        Button info_register = findViewById(R.id.info_register);
        EditText info_nickname = findViewById(R.id.info_nickname);
        EditText info_age = findViewById(R.id.info_age);
        EditText info_city = findViewById(R.id.info_city);
        Intent intent = getIntent();
        String phone = intent.getStringExtra("phone");
        String code = intent.getStringExtra("code");
        info_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nickname = info_nickname.getText().toString().trim();
                String age = info_age.getText().toString().trim();
                String city = info_city.getText().toString().trim();
                if (nickname.equals("")){
                    Toast.makeText(getApplicationContext(), "昵称不能为空", Toast.LENGTH_SHORT).show();
                }else if(age.equals("") || !isNumeric(age)){
                    Toast.makeText(getApplicationContext(), "年龄不能为空或年龄不是数字", Toast.LENGTH_SHORT).show();
                }else if(city.equals("")){
                    Toast.makeText(getApplicationContext(), "城市不能为空", Toast.LENGTH_SHORT).show();
                }else if(sex == 0){
                    Toast.makeText(getApplicationContext(), "请选择性别", Toast.LENGTH_SHORT).show();
                }else {
                    String url = UrlUtil.URL + "/register?phone=" + phone + "&code=" + code + "&age=" + age + "&city=" + city + "&nickname=" + nickname + "&sex=" + sex;
                    RequestQueue requestQueue= Volley.newRequestQueue(InfoActivity.this);
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {
                                Map<String, Object> map = JsonUtil.jsonToMap(s.toString());
                                Map<String, Object> data = (Map<String, Object>) map.get("data");
                                Integer code1 = (Integer) map.get("code");
                                if(code1 == 0){
                                    Toast.makeText(InfoActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent1 = new Intent(InfoActivity.this, IndexActivity.class);
                                    intent1.putExtra("data", (Serializable) data);
                                    startActivity(intent1);
                                    finish();
                                }else{
                                    String message = (String) map.get("message");
                                    Toast.makeText(InfoActivity.this, message, Toast.LENGTH_SHORT).show();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Log.d("错误", volleyError.toString());
                            Toast.makeText(InfoActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
                        }
                    }) {

                        @Override
                        protected Response<String> parseNetworkResponse(NetworkResponse response) {

                            String sesseionid = new ConnectViaSession(InfoActivity.this).GetSession();
                            new ConnectViaSession(InfoActivity.this).getSession(response);
                            return super.parseNetworkResponse(response);
                        }};
                    requestQueue.add(stringRequest);

                }
            }
        });



    }


    private void checkReturn() {
        Button info_return = findViewById(R.id.user_info_return);
        info_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }


}
